/*
 * PingPPService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.APIConnectionException;
import com.pingplusplus.exception.APIException;
import com.pingplusplus.exception.AuthenticationException;
import com.pingplusplus.exception.InvalidRequestException;
import com.pingplusplus.exception.PingppException;
import com.pingplusplus.model.App;
import com.pingplusplus.model.Charge;
import com.pingplusplus.model.ChargeCollection;

/**
 * 
 *
 * @author kiri
 * @date 2015年5月17日
 * @version 1.0
 */
@Service
public class PingPPService extends AbstractService {

	public static final String CHANNEL_ALIPAY = "alipay";

	/**
	 * pingpp 管理平台对应的API key
	 */
	public static String apiKey = "sk_live_qbmSlWLpDENvsSrJhNGwvapy";
	// public static String apiKey = "sk_test_CmD8u5mvHuvLjbvL0KKOWHaT";

	/**
	 * pingpp 管理平台对应的应用I
	 */
	public static String appId = "app_H00KqTWb9yjLLK0q";

	/**
	 * 创建Charge
	 * @return
	 */
	public Charge createCharge(String orderNo, Integer amount, String subject, String body, String channel) {
		Pingpp.apiKey = apiKey;

		Charge charge = null;
		Map<String, Object> chargeMap = new HashMap<String, Object>();
		chargeMap.put("amount", amount);
		chargeMap.put("currency", "cny");
		chargeMap.put("subject", subject);
		chargeMap.put("body", body);
		chargeMap.put("order_no", orderNo);
		chargeMap.put("channel", channel);
		chargeMap.put("client_ip", "127.0.0.1");
		Map<String, String> app = new HashMap<String, String>();
		app.put("id", appId);
		chargeMap.put("app", app);
		try {
			// 发起交易请求
			charge = Charge.create(chargeMap);
			System.out.println(charge);
		}
		catch (PingppException e) {
			e.printStackTrace();
		}
		return charge;
	}

	/**
	 * 查询Charge
	 * @param id
	 */
	public void retrieve(String id) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			List<String> expande = new ArrayList<String>();
			expande.add("app");
			param.put("expand", expande);
			// Charge charge = Charge.retrieve(id);
			// Expand app
			Charge charge = Charge.retrieve(id, param);
			if (charge.getApp() instanceof App) {
				App app = (App) charge.getApp();
				System.out.println("App Object ,appId = " + app.getId());
			} else {
				System.out.println("String ,appId = " + charge.getApp());
			}

			System.out.println(charge);

		}
		catch (PingppException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 分页查询Charge
	 * @return
	 */
	public ChargeCollection all() {
		ChargeCollection chargeCollection = null;
		Map<String, Object> chargeParams = new HashMap<String, Object>();
		chargeParams.put("limit", 3);

		// 增加此处设施，刻意获取app expande
		// List<String> expande = new ArrayList<String>();
		// expande.add("app");
		// chargeParams.put("expand", expande);

		try {
			chargeCollection = Charge.all(chargeParams);
			System.out.println(chargeCollection);
		}
		catch (AuthenticationException e) {
			e.printStackTrace();
		}
		catch (InvalidRequestException e) {
			e.printStackTrace();
		}
		catch (APIConnectionException e) {
			e.printStackTrace();
		}
		catch (APIException e) {
			e.printStackTrace();
		}
		return chargeCollection;
	}

}
